﻿using System;
using System.Collections.Generic;
using System.IO;
using SimpleFS.Core;
using SimpleFS.Storage.Windows;
using SimpleFS.Services.Encryption;
using System.Security.Cryptography;
using NUnit.Framework;

namespace SimpleFS.Specs.Services.Encryption
{
    [TestFixture]
    public class FileTest
    {
        public static void Main(string[] args)
        {
            FileTest test = new FileTest();
            test.TestWrite();
        }

        [Test]
        public void TestRead()
        {
            string fileName = "../../Resources/Hello.txt";

            SymmetricAlgorithm symmetricAlgorithm =
                SymmetricAlgorithm.Create();
            ICryptoTransform transform = symmetricAlgorithm.CreateEncryptor();

            SimpleFS.Storage.Windows.File windowsFile
                = new SimpleFS.Storage.Windows.File(fileName);
            SimpleFS.Services.Encryption.File file
                = new SimpleFS.Services.Encryption.File(windowsFile, transform);

            Stream stream = file.Read();

            stream.Close();
        }

        [Test]
        public void TestWrite()
        {
            string fileName = "../../Resources/Hello.txt";

            SymmetricAlgorithm symmetricAlgorithm =
                SymmetricAlgorithm.Create();
            ICryptoTransform transform = symmetricAlgorithm.CreateEncryptor();
            
            FileStream source = System.IO.File.Open(fileName, FileMode.Open);    
            byte[] buffer = new byte[source.Length];
            MemoryStream ms = new MemoryStream();
            ms.Write(buffer, 0, (int)source.Length);
            source.Close();

            SimpleFS.Storage.Windows.File windowsFile 
                = new SimpleFS.Storage.Windows.File(fileName);
            SimpleFS.Services.Encryption.File file
                = new SimpleFS.Services.Encryption.File(windowsFile, transform);

            file.Write(ms);

            Stream unencrypted = windowsFile.Read();

            ms.Close();
        }
    }
}
